var mongoose = require('mongoose');
var Schema = mongoose.Schema;

mongoose.connect("mongodb://localhost:27020/makeblock-iot");

var Users = new Schema({
	userid: Number,
	userkey:String,
	username:String,
	time:Date
}/*,{ minimize: false }*/);

//CRUD Operations
//Insert
/**
 * example:
 * var userid 	  = {userid: 1};
 * var update     = {$set : {userkey : "linyanwen"}};
 * var options    = {upsert : true};
 * var callback   = function(error);
 */
Users.methods.Insert = function(userid, update, option, callback) {
	return this.model('makeblockusers').update(userid, update, option, callback);
}

//Delete
/**
 * example:
 * var userid 	  = {userid: 1};
 * var callback   = function(error);
 */
Users.methods.Delete = function(userid, callback) {
	return this.model('makeblockusers').remove(userid, callback);
}

//Update
/**
 * example:
 * var userid 	  = {userid: 1};
 * var update     = {$set : {userkey : "linyanwen"}};
 * var options    = {upsert : true};
 * var callback   = function(error);
 */
Users.methods.Update = function(userid, update, option, callback) {
	return this.model('makeblockusers').update(userid, update, option, callback);
}

//Query
/**
 * example:
 * var userid 	  = {userid: 1};
 * var callback   = function(error);
 */
Users.methods.Query = function(userid, callback) {
	return this.model('makeblockusers').findOne(userid, callback);
}


//Static method
Users.statics.Insert = function(userid, update, option, callback) {
	return this.model('makeblockusers').update(userid, update, option, callback);
}
Users.statics.Delete = function(userid, callback) {
	return this.model('makeblockusers').remove(userid, callback);
}
Users.statics.Update = function(userid, update, option, callback) {
	return this.model('makeblockusers').update(userid, update, option, callback);
}
Users.statics.Query = function(userid, callback) {
	return this.model('makeblockusers').findOne(userid, callback);
}

exports.Users=mongoose.model('makeblockusers', Users);
